<!DOCTYPE HTML>
<html lang="en-US" >
    
    <head>
        
        <meta charset="UTF-8">
        <title>id类型 | OC中的私有方法</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 1.0.3">
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
        
    
    
    
    <link rel="next" href="../new/README.html" />
    
    
    <link rel="prev" href="../6.html" />
    

        
    </head>
    <body>
        
        
<link rel="stylesheet" href="../gitbook/style.css">


        
    <div class="book"  data-level="5" data-basepath=".." data-revision="1442488483454">
    

<div class="book-summary">
    <div class="book-search">
        <input type="text" placeholder="Type to search" class="form-control" />
    </div>
    <ul class="summary">
        
    	
    	
    	

        

        
    
        
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../index.html">
                        <i class="fa fa-check"></i>
                        
                         OC中的私有方法 
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="1" data-path="property/README.html">
            
                
                    <a href="../property/README.html">
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                         @property基本概念 
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="2" data-path="synthesize/README.html">
            
                
                    <a href="../synthesize/README.html">
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                         @synthesize基本概念
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="3" data-path="5.html">
            
                
                    <a href="../5.html">
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                         @property增强
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="4" data-path="6.html">
            
                
                    <a href="../6.html">
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                         @property修饰符
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter active" data-level="5" data-path="id/README.html">
            
                
                    <a href="../id/README.html">
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                         id类型
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="6" data-path="new/README.html">
            
                
                    <a href="../new/README.html">
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                         new方法实现原理
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="7" data-path="9.html">
            
                
                    <a href="../9.html">
                        <i class="fa fa-check"></i>
                        
                            <b>7.</b>
                        
                         构造方法
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="8" data-path="10.html">
            
                
                    <a href="../10.html">
                        <i class="fa fa-check"></i>
                        
                            <b>8.</b>
                        
                         自定义构造方法
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="9" data-path="12.html">
            
                
                    <a href="../12.html">
                        <i class="fa fa-check"></i>
                        
                            <b>9.</b>
                        
                         继承中的自定义构造方法
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="10" data-path="13.html">
            
                
                    <a href="../13.html">
                        <i class="fa fa-check"></i>
                        
                            <b>10.</b>
                        
                         自定义类工厂方法
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="11" data-path="1.html">
            
                
                    <a href="../1.html">
                        <i class="fa fa-check"></i>
                        
                            <b>11.</b>
                        
                         类的本质
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="12" data-path="14.html">
            
                
                    <a href="../14.html">
                        <i class="fa fa-check"></i>
                        
                            <b>12.</b>
                        
                         类的启动过程
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="13" data-path="sel/README.html">
            
                
                    <a href="../sel/README.html">
                        <i class="fa fa-check"></i>
                        
                            <b>13.</b>
                        
                         SEL类型
                    </a>
                
            
            
        </li>
    


        
        <li class="divider"></li>
        <li>
            <a href="http://www.gitbook.io/" target="blank" class="gitbook-link">Published using GitBook</a>
        </li>
        
    </ul>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header">
    <!-- Actions Left -->
    <a href="#" class="btn pull-left toggle-summary" aria-label="Toggle summary"><i class="fa fa-align-justify"></i></a>
    <a href="#" class="btn pull-left toggle-search" aria-label="Toggle search"><i class="fa fa-search"></i></a>
    
    <div id="font-settings-wrapper" class="dropdown pull-left">
        <a href="#" class="btn toggle-dropdown" aria-label="Toggle font settings"><i class="fa fa-font"></i>
        </a>
        <div class="dropdown-menu font-settings">
    <div class="dropdown-caret">
        <span class="caret-outer"></span>
        <span class="caret-inner"></span>
    </div>

    <div class="buttons">
        <button type="button" id="reduce-font-size" class="button size-2">A</button>
        <button type="button" id="enlarge-font-size" class="button size-2">A</button>
    </div>

    <div class="buttons font-family-list">
        <button type="button" data-font="0" class="button">Serif</button>
        <button type="button" data-font="1" class="button">Sans</button>
    </div>

    <div class="buttons color-theme-list">
        <button type="button" id="color-theme-preview-0" class="button size-3" data-theme="0">White</button>
        <button type="button" id="color-theme-preview-1" class="button size-3" data-theme="1">Sepia</button>
        <button type="button" id="color-theme-preview-2" class="button size-3" data-theme="2">Night</button>
    </div>
</div>

    </div>

    <!-- Actions Right -->
    
    <div class="dropdown pull-right">
        <a href="#" class="btn toggle-dropdown" aria-label="Toggle share dropdown"><i class="fa fa-share-alt"></i>
        </a>
        <div class="dropdown-menu font-settings dropdown-left">
            <div class="dropdown-caret">
                <span class="caret-outer"></span>
                <span class="caret-inner"></span>
            </div>
            <div class="buttons">
                <button type="button" data-sharing="twitter" class="button">Twitter</button>
                <button type="button" data-sharing="google-plus" class="button">Google</button>
                <button type="button" data-sharing="facebook" class="button">Facebook</button>
                <button type="button" data-sharing="weibo" class="button">Weibo</button>
                <button type="button" data-sharing="instapaper" class="button">Instapaper</button>
            </div>
        </div>
    </div>
    

    
    <a href="#" target="_blank" class="btn pull-right google-plus-sharing-link sharing-link" data-sharing="google-plus" aria-label="Share on Google Plus"><i class="fa fa-google-plus"></i></a>
    
    
    <a href="#" target="_blank" class="btn pull-right facebook-sharing-link sharing-link" data-sharing="facebook" aria-label="Share on Facebook"><i class="fa fa-facebook"></i></a>
    
    
    <a href="#" target="_blank" class="btn pull-right twitter-sharing-link sharing-link" data-sharing="twitter" aria-label="Share on Twitter"><i class="fa fa-twitter"></i></a>
    
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../" >OC中的私有方法</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-gitbook_349">
                    
                        <h1 id="id">id类型</h1>
<h2 id="">本小节知识:</h2>
<ol>
<li>【理解】静态类型和动态类型</li>
<li>【理解】为什么要有动态类型?</li>
<li>【理解】id数据类型与静态类型</li>
</ol>
<hr>
<h2 id="1">1.静态类型和动态类型</h2>
<ul>
<li>静态类型<ul>
<li>将一个指针变量定义为特定类的对象时,使用的是静态类型,在编译的时候就知道这个指针变量所属的类,这个变量总是存储特定类的对象。</li>
</ul>
</li>
</ul>
<pre><code class="lang-objc">Person *p = [Person new];
</code></pre>
<ul>
<li>动态类型<ul>
<li>这一特性是程序直到执行时才确定对象所属的类</li>
</ul>
</li>
</ul>
<pre><code class="lang-objc"><span class="hljs-keyword">id</span> obj = [Person new];
</code></pre>
<hr>
<h2 id="2">2.为什么要有动态类型?</h2>
<ul>
<li>我们知道NSObject是OC中的基类</li>
<li>那么任何对象的NSObject类型的指针可以指向任意对象，都没有问题</li>
<li>但是NSObject是静态类型，如果通过它直接调用NSObject上面不存在的方法，编译器会报错。</li>
<li>你如果想通过NSObject的指针调用特定对象的方法，就必须把NSObject * 这种类型强转成特定类型。然后调用。如下</li>
</ul>
<pre><code class="lang-objc"><span class="hljs-comment">//定义NSObject * 类型</span>
 <span class="hljs-built_in">NSObject</span>* obj = [Cat new];
 Cat *c = (Cat*)obj;
 [c eat];
</code></pre>
<ul>
<li>id 是一种通用的对象类型,它可以指向属于任何类的对象,也可以理解为万能指针 ,相当于C语言的 void *</li>
<li>因为id是动态类型,所以可以通过id类型直接调用指向对象中的方法, 编译器不会报错</li>
</ul>
<pre><code class="lang-objc"><span class="hljs-comment">/// Represents an instance of a class.</span>
<span class="hljs-keyword">struct</span> objc_object {
    Class isa  OBJC_ISA_AVAILABILITY;
};

<span class="hljs-comment">/// A pointer to an instance of a class.</span>
<span class="hljs-keyword">typedef</span> <span class="hljs-keyword">struct</span> objc_object *<span class="hljs-keyword">id</span>;
</code></pre>
<pre><code class="lang-objc"> <span class="hljs-keyword">id</span> obj = [C at new];
 [obj eat]; <span class="hljs-comment">// 不用强制类型转换</span>

 [obj test]; <span class="hljs-comment">//可以调用私有方法</span>
</code></pre>
<ul>
<li>注意：<ul>
<li>在id的定义中,已经包好了*号。id指针只能指向OC中的对象</li>
<li>为了尽可能的减少编程中出错，Xcode做了一个检查，当使用id 类型的调用本项目中所有类中都没有的方法，编译器会报错</li>
<li>id类型不能使用.语法, 因为.语法是编译器特性, 而id是运行时特性</li>
</ul>
</li>
</ul>
<h2 id="3id">3.id数据类型与静态类型</h2>
<ul>
<li><p>虽然说id数据类型可以存储任何类型的对象，但是不要养成滥用这种通用类型</p>
<ul>
<li>如没有使用到多态尽量使用静态类型</li>
<li>静态类型可以更早的发现错误(在编译阶段而不是运行阶段)</li>
<li>静态类型能够提高程序的可读性</li>
<li>使用动态类型前最好判断其真实类型</li>
</ul>
</li>
<li><p>动态类型判断类型</p>
<ul>
<li>- (BOOL)isKindOfClass:classObj 判断实例对象是否是这个类或者这个类的子类的实例</li>
</ul>
</li>
</ul>
<pre><code class="lang-objc">    Person *p = [Person new];
    Student *stu = [Student new];

    <span class="hljs-built_in">BOOL</span> res = [p isKindOfClass:[Person class]];
    <span class="hljs-built_in">NSLog</span>(<span class="hljs-string">@"res = %i"</span>, res); <span class="hljs-comment">// YES</span>
    res = [stu isKindOfClass:[Person class]];
    <span class="hljs-built_in">NSLog</span>(<span class="hljs-string">@"res = %i"</span>, res); <span class="hljs-comment">// YES</span>
</code></pre>
<pre><code>- (BOOL) isMemberOfClass: classObj 判断是否是这个类的实例
</code></pre><pre><code class="lang-objc">    Person *p = [Person new];
    Student *stu = [Student new];

    <span class="hljs-built_in">BOOL</span> res = [p isMemberOfClass:[Person class]];
    <span class="hljs-built_in">NSLog</span>(<span class="hljs-string">@"res = %i"</span>, res); <span class="hljs-comment">// YES</span>
    res = [stu isMemberOfClass:[Person class]];
    <span class="hljs-built_in">NSLog</span>(<span class="hljs-string">@"res = %i"</span>, res); <span class="hljs-comment">// NO</span>
</code></pre>
<pre><code>+ (BOOL) isSubclassOfClass:classObj 判断类是否是指定类的子类
</code></pre><pre><code class="lang-objc">    <span class="hljs-built_in">BOOL</span> res = [Person isSubclassOfClass:[Student class]];
    <span class="hljs-built_in">NSLog</span>(<span class="hljs-string">@"res = %i"</span>, res); <span class="hljs-comment">// NO</span>

    res = [Student isSubclassOfClass:[Person class]];
    <span class="hljs-built_in">NSLog</span>(<span class="hljs-string">@"res = %i"</span>, res); <span class="hljs-comment">// YES</span>
</code></pre>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../6.html" class="navigation navigation-prev " aria-label="Previous page: @property修饰符"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../new/README.html" class="navigation navigation-next " aria-label="Next page: new方法实现原理"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../gitbook/app.js"></script>

    
    <script src="https://cdn.mathjax.org/mathjax/2.4-latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-mathjax/plugin.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"fontSettings":{"theme":null,"family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        
    </body>
    
</html>
